package com.zy.controller;

import com.zy.bean.TMenu;
import com.zy.service.TMenuService;
import com.zy.service.TPermissionMenuService;
import com.zy.util.Datas;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Author: zy 690748807@qq.com
 * @Description: TODO
 * @Date: 2023/12/10 20:12
 * @Version: 1.0
 * @name 方法名字： 菜单列表
 */
@Controller
public class TmenuController {
    @Autowired
    TMenuService tMenuService;
    @Autowired
    TPermissionMenuService tPermissionMenuService;
    Logger log = LoggerFactory.getLogger(TmenuController.class);

    /**
     * @Author: zy 690748807@qq.com
     * @Param:
     * @return:
     * @Date: 2023/12/10
     * @Description: 1.跳转到菜单页面
     */
    @RequestMapping("/menu/index")
    public String index() {
        return "/menu/index";
    }

    /**
     * @Author: zy 690748807@qq.com
     * @Param:
     * @return:
     * @Date: 2023/12/12
     * @Description: 2.加载菜单树列表
     */
    @ResponseBody
    @RequestMapping("/menu/loadTree")
    public List<TMenu> loadTree() {
        List<TMenu> list = tMenuService.getListMenuAll();
        return list;
    }

    /**
     * @Author: zy 690748807@qq.com
     * @Param:
     * @return:
     * @Date: 2023/12/16
     * @Description: 添加菜单
     */
    @ResponseBody
    @RequestMapping("/menu/doAdd")
    public String doAdd(TMenu tMenu) {
        Integer integer = tMenuService.addTmenu(tMenu);
        if (integer > 0) {
            return "ok";
        }
        return "error";
    }

    /**
     * @Author: zy 690748807@qq.com
     * @Param:
     * @return:
     * @Date: 2023/12/16
     * @Description: 获取菜单信息
     */
    @ResponseBody
    @RequestMapping("/menu/getMenuById")
    public TMenu getMenuById(Integer id) {
        TMenu menu = tMenuService.getMenuById(id);
        return menu;
    }

    /**
     * @Author: zy 690748807@qq.com
     * @Param:
     * @return:
     * @Date: 2023/12/16
     * @Description: 修改菜单信息
     */
    @ResponseBody
    @RequestMapping("/menu/doUpdate")
    public String updateTree(TMenu tMenu) {
        Integer integer = tMenuService.updateTree(tMenu);
        if (integer > 0) {
            return "ok";
        }
        return "error";
    }

    /**
     * @Author: zy 690748807@qq.com
     * @Param:
     * @return:
     * @Date: 2023/12/16
     * @Description: 删除菜单信息
     */
    @ResponseBody
    @RequestMapping("/menu/doDelete")
    public String delTree(Integer id) {
        Integer integer = tMenuService.delTree(id);
        return "ok";
    }

    /**
     * @Author: zy 690748807@qq.com
     * @Param:var 注意参数====  data = {mid:tempMenuid,perIds:idsStr};Integer[] roleId, String adminId
     * @return:
     * @Date: 2024/1/20
     * @Description: 菜单页面添加权限
     */
    @ResponseBody
    @PostMapping("/menu/assignPermissionToMenu")
    public String assignPermissionToMenu( Integer[] perIds ,Integer mid) {

        log.debug("mid={}", mid);
        for (Integer perId : perIds) {
            log.debug("perIds={}", perId);
        }

        Integer integer = tPermissionMenuService.savePermissionToMenu(perIds,mid);
        if (integer > 0) {
            return "ok";
        } else {
            return "error";
        }

    }

}
